package com.cj.tool.utils;

import android.content.Context;
import android.graphics.drawable.PictureDrawable;
import android.net.Uri;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.cj.tool.R;
import com.cj.tool.svg.GlideApp;
import com.cj.tool.svg.SvgSoftwareLayerSetter;

import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;

/**
 * 图片加载
 * auth zengyi
 * created at 2017/11/4 17:00
 */

public class GlideUtils {

    private static RequestOptions options = new RequestOptions()
            //.placeholder(R.drawable.img_default)
            .error(R.mipmap.ic_launcher)
            .centerCrop()
            .diskCacheStrategy(DiskCacheStrategy.ALL);

    private static RequestOptions options4 = new RequestOptions()
            //.placeholder(R.drawable.img_default)
            .error(R.mipmap.ic_launcher)
            .fitCenter()
            .diskCacheStrategy(DiskCacheStrategy.ALL);
    private static RequestOptions mOptions =
            RequestOptions.bitmapTransform(new RoundedCorners(8));


    public static void setImageCenter(Context context, String imgPath, ImageView imageView) {
        Glide.with(context).load(imgPath).apply(options).into(imageView);
    }

    public static void setImageCenter(Context context, int res, ImageView imageView) {
        Glide.with(context).load(res).apply(options).into(imageView);
    }

    public static void setImageCenterInside(Context context, String imgPath, ImageView imageView) {
        Glide.with(context).load(imgPath).apply(options4).into(imageView);
    }

    public static void imageloadCircleCrop8(Context context, String imgPath, ImageView imageView) {
        Glide.with(context).load(imgPath).apply(mOptions).into(imageView);
    }

    public static void imageloadCircleCrop8(Context context, int res, ImageView imageView) {
        Glide.with(context).load(res).apply(mOptions).into(imageView);
    }

    public static void imageLoadSvg(Context context, Object url, ImageView imageView) {
        RequestBuilder<PictureDrawable> requestBuilder = GlideApp.with(context)
                .as(PictureDrawable.class)
                //.placeholder(R.drawable.img_default)
                //.error(R.drawable.img_default)
                .transition(withCrossFade())
                .listener(new SvgSoftwareLayerSetter());
        if (url instanceof Uri) {
            requestBuilder.load(url).into(imageView);
        } else if (url instanceof String) {
            requestBuilder.load(Uri.parse((String) url)).into(imageView);
        }


    }
}